package com.flute.framework.monitor;

import java.util.Date;
import java.util.HashMap;

import com.flute.framework.monitor.MonitedValue.MonitorLevel;
import com.flute.tools.util.StringUtil;

public class MonitedValueUtil {
	private static final String OBJECT_FIELD_SPLITER = "Й";
	private static final String MAP_FIELD_SPLITER = "К";
	private static final String LINE_REPLACEMENT = "Л";

	private static HashMap<MonitorLevel, Integer> levelValuemap;
	private static HashMap<Integer, MonitorLevel> valueLevelmap;

	static {
		levelValuemap = new HashMap<MonitorLevel, Integer>();
		valueLevelmap = new HashMap<Integer, MonitorLevel>();

		levelValuemap.put(MonitorLevel.all, 1);
		valueLevelmap.put(1, MonitorLevel.all);

		levelValuemap.put(MonitorLevel.debug, 2);
		valueLevelmap.put(2, MonitorLevel.debug);

		levelValuemap.put(MonitorLevel.info, 3);
		valueLevelmap.put(3, MonitorLevel.info);

		levelValuemap.put(MonitorLevel.warn, 4);
		valueLevelmap.put(4, MonitorLevel.warn);

		levelValuemap.put(MonitorLevel.error, 5);
		valueLevelmap.put(5, MonitorLevel.error);

	}

	public static String toLogString(MonitedValue value) {
		if (value != null) {
			StringBuffer sb = new StringBuffer();
			sb.append(value.getEventId());
			sb.append(OBJECT_FIELD_SPLITER);
			sb.append(getLineReplaced(value.getKey()));
			sb.append(OBJECT_FIELD_SPLITER);
			sb.append(levelValuemap.get(value.getLevel()));
			sb.append(OBJECT_FIELD_SPLITER);
			sb.append(value.getDate().getTime());
			sb.append(OBJECT_FIELD_SPLITER);
			String[] fields = value.getFields();
			if (fields.length > 0) {
				sb.append(fields[0]);
				sb.append(MAP_FIELD_SPLITER);
				sb.append(getLineReplaced(value.getFieldValue(fields[0])));
				for (int i = 1; i < fields.length; i++) {
					sb.append(MAP_FIELD_SPLITER);
					sb.append(getLineReplaced(fields[i]));
					sb.append(MAP_FIELD_SPLITER);
					sb.append(getLineReplaced(value.getFieldValue(fields[i])));
				}
			}

			return sb.toString().trim();
		}

		return "";
	}

	private static String getLineReplaced(String string) {
		if (StringUtil.isNullOrEmpty(string)) {
			return "";
		} else {
			return string.replace("\n", LINE_REPLACEMENT);
		}
	}

	private static String getOrignalString(String string) {
		if (StringUtil.isNullOrEmpty(string)) {
			return "";
		} else {
			return string.replace(LINE_REPLACEMENT, "\n");
		}
	}

	public static MonitedValue fromLogString(String string) {
		if (StringUtil.isNullOrEmpty(string)) {
			return null;
		} else {
			String[] objFs = string.split(OBJECT_FIELD_SPLITER);
			if (objFs.length == 5) {
				try {
					MonitedValue mv = new MonitedValue();
					mv.setEventId(Long.valueOf(objFs[0]));
					mv.setKey(getOrignalString(objFs[1]));
					mv.setLevel(valueLevelmap.get(Integer.valueOf(objFs[2])));
					mv.setDate(new Date(Long.valueOf(objFs[3])));
					String[] mapFs = objFs[4].split(MAP_FIELD_SPLITER);
					if (mapFs.length % 2 == 0) {
						for (int i = 0; i < mapFs.length; i = i + 2) {
							mv.addFieldValue(getOrignalString(mapFs[i]),
									getOrignalString(mapFs[i + 1]));
						}
					}

					return mv;
				} catch (Exception e) {
					e.printStackTrace();
				}

				return null;
			} else {
				return null;
			}
		}
	}
}
